#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "Menu.h"
#include "MenuMain.h"
#include "MenuExtra.h"

//Screen
const int WIDTH  = 640;
const int HEIGHT = 480;

int main( int argc, char* args[])
{
	//Rendering window
	SDL_Window* window = NULL;
	SDL_Surface* screen = NULL;

	//Initialize SDL
	if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) return -1;

	//Create window
	window = SDL_CreateWindow( "Ritle", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN );
	if (window == NULL) return -1;

	//Initialize PNG
	if ( !(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) ) return -1;

	//Create screen
	screen = SDL_GetWindowSurface( window );

	//Menu
	//Background background = new Background;
	Menu* menu = new MenuMain;
	menu->initialize();

	//Main loop
	bool running = true;
	SDL_Event e;
	while (running)
	{
		//Input
		while (SDL_PollEvent(&e) != 0)
		{
			if (e.type == SDL_QUIT) running = false; else
			if (e.type == SDL_KEYDOWN)
			{
				switch (e.key.keysym.sym)
				{
				case SDLK_UP:
					running = false;
					break;
				case SDLK_RIGHT:
					menu = new MenuExtra;
					menu->initialize();
					break;
				}
			}
		}

		//Update
		menu->update();

		//Draw
		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 254 ,158, 112));
		menu->draw(screen);
		SDL_UpdateWindowSurface(window);
	}

	//Close game
	for (int i = 0; i < 36; i++)
	{
	//	SDL_FreeSurface(image[i]);
	//	image[i] = NULL;
	}

	//Destroy window
	SDL_DestroyWindow( window );
	window = NULL;

	//Quit SDL
	SDL_Quit();

	return 0;
}
